Reusing loop variables

Every fiber of my soul is in an uproar over this, yet the code works.

I have inherited code that looks like this:

Link lnk
Object o1, o2
Module m1 = current

for o1 in m1 do
{  for lnk in o1 ->"*" do
   {  o2 = target object of lnk
      for lnk in o2 ->"*" do
      {  o2 = target object of lnk
         for lnk in o2 ->"*" do
         {  deal with o2
         }
      }
   }
}
Note variable "lnk" is used as loop control in 3 nested loops, and variable "o2" used in 2 nested loops.

The correct objects are being visited in the right order, all links are considered.  This code works exactly the same when I add variable "lnk2" for the 2 loop, "lnk3" and "o3" for the 3rd.

It feels like the natural laws of physics don't apply.

-Louie


llandale - Wed Jan 14 14:40:53 EST 2015

Re: Reusing loop variables
Mathias Mamsch - Thu Jan 15 03:02:09 EST 2015

Wow. I can see why this works, but I am equally astonished ;-) Seems like this is one of the "do it - because you can" things ...